package com.swust.excel.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.swust.excel.entity.LogInfo;
import com.swust.excel.support.DataSourceSupport;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.time.LocalDate;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author: Chen Yixing
 * @since: 2023/11/17 10:48:37
 * @description: zip返回多sheet
 */
@RestController
@RequestMapping("/export/excel")
public class ZipSheetController {
    private final DataSourceSupport dataSourceSupport;

    public ZipSheetController(DataSourceSupport dataSourceSupport) {
        this.dataSourceSupport = dataSourceSupport;
    }

    /**
     * 导出多sheet案例
     * 方法：
     * 1 EasyExcel读取的数据放到 ByteArrayOutputStream bos 中
     * 2 将 response.getOutputStream() 输出流放入 ZipOutputStream zos 压缩流初始化的参数中
     * 3 将 bos 读取到的字节数组 压入 ZipOutputStream
     * 4 最后，刷新并关闭 bos 与 zos 流
     * 综上，流与流之间的交互，是通过“字节数组”进行通讯的。
     */
    @GetMapping("/zipMorSheet")
    public void exportMorSheet(HttpServletResponse response) throws Exception {
        List<LogInfo> logs = dataSourceSupport.initLogInfo();
        if (ObjectUtils.isEmpty(logs)) {
            dataSourceSupport.initFailResponse(response);
        } else {
            dataSourceSupport.initZipExcelResponse(response, LocalDate.now().toString());

            ByteArrayOutputStream bos = new ByteArrayOutputStream();//字节流
            // 表一写入
            ExcelWriter writer = EasyExcel.write(bos, LogInfo.class).build();
            WriteSheet sheet = EasyExcel.writerSheet(0, "基础信息").build();
            writer.write(logs, sheet);
            // 表二写入
            WriteSheet sheet2 = EasyExcel.writerSheet(1, "详细信息").head(LogInfo.class).build();
            writer.write(logs, sheet2);
            // 关闭流，response返回excel流
            writer.finish();

            ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
            zipOutputStream.putNextEntry(new ZipEntry(LocalDate.now().toString() + ".xlsx"));
            zipOutputStream.write(bos.toByteArray());
            zipOutputStream.flush();
            zipOutputStream.close();
        }
    }

    @GetMapping("/zipFillMorSheet")
    public void zipFillMorSheet(HttpServletResponse response) throws Exception {
        List<LogInfo> logs = dataSourceSupport.initLogInfo();
        if (ObjectUtils.isEmpty(logs)) {
            dataSourceSupport.initFailResponse(response);
        } else {
            dataSourceSupport.initZipExcelResponse(response, LocalDate.now().toString());

            ByteArrayOutputStream bos1 = new ByteArrayOutputStream();//字节流
            // 表一写入
            ExcelWriter writer1 = EasyExcel.write(bos1, LogInfo.class).build();
            WriteSheet sheet1 = EasyExcel.writerSheet(0, "基础信息").build();
            writer1.write(logs, sheet1);
            // 关闭流，response返回excel流
            writer1.finish();

            ByteArrayOutputStream bos2 = new ByteArrayOutputStream();//字节流
            // 表二写入
            ExcelWriter writer2 = EasyExcel.write(bos2, LogInfo.class).build();
            WriteSheet sheet2 = EasyExcel.writerSheet(0, "基础信息").build();
            writer2.write(logs, sheet2);
            // 关闭流，response返回excel流
            writer2.finish();

            ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
            zipOutputStream.putNextEntry(new ZipEntry("表一.xlsx"));
            zipOutputStream.write(bos1.toByteArray());
            zipOutputStream.putNextEntry(new ZipEntry("表二.xlsx"));
            zipOutputStream.write(bos2.toByteArray());

            zipOutputStream.flush();
            zipOutputStream.close();
        }
    }

}
